package cn.woblog.androidhttputilssample;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import cn.woblog.android.httputils.callback.impl.FileCallback;
import cn.woblog.android.httputils.callback.impl.JsonCallback;
import cn.woblog.android.httputils.callback.impl.StringCallback;
import cn.woblog.android.httputils.domain.Request;
import cn.woblog.android.httputils.domain.Request.RequestMethod;
import cn.woblog.android.httputils.domain.Request.RequestTool;
import cn.woblog.android.httputils.exception.AppException;
import cn.woblog.android.httputils.manager.RequestManager;
import cn.woblog.android.httputils.utils.Logs;
import cn.woblog.androidhttputilssample.domain.Model;

public class MainActivity extends Activity {

	protected static final String TAG = "MainActivity";
	protected static final String TAG1 = "download1";
	protected static final String TAG2 = "download2";
	private TextView tv;
	private RequestManager mRequestManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tv = (TextView) findViewById(R.id.tv);
		mRequestManager = RequestManager.getInstance();
	}

	public void getstring(View view) {
		// Request request = new Request(
		// "http://192.168.1.101/androidtest/module.html");
		String path = Environment.getExternalStorageDirectory()
				.getAbsolutePath() + File.separator + "get.txt";
		final Request request = new Request(
				"http://bcs.duapp.com/ttttttttttttttt/module.json?sign=MBO:08fa4d126ce0e0481f207d607a2be537:tJMVODZxlsxrpFm8zrW%2FY6nSkXE%3D");
		request.tool = RequestTool.HTTPURLCONNECTION;
		// request.addHeader("Content-Type", "*/*");
		request.setCallBack(new StringCallback() {

			@Override
			public String preRequest() {
				// query data for database
				request.url = "http://bcs.duapp.com/ttttttttttttttt/moduleClass.json";
				return super.preRequest();
			}

			@Override
			public String postRequest(String t) {
				// insert to database
				// compare
				return super.postRequest(t);
			}

			@Override
			public void onSuccess(String object) {
				Toast.makeText(MainActivity.this, (String) object, 1).show();
			}

			@Override
			public void onFailure(AppException exception) {
				exception.printStackTrace();
				Toast.makeText(MainActivity.this, "请求失败", 0).show();
			}

		});
		request.execute();
	}

	public void getjsonToList(View view) {
		// Request request = new Request(
		// "http://bcs.duapp.com/ttttttttttttttt/module.json?sign=MBO:08fa4d126ce0e0481f207d607a2be537:tJMVODZxlsxrpFm8zrW%2FY6nSkXE%3D");
		// // request.addHeader("Content-Type", "*/*");
		// request.setCallBack(new JsonCallback<ArrayList<Model>>() {
		//
		// @Override
		// public void onFailure(Exception exception) {
		// Toast.makeText(MainActivity.this, "请求失败", 0).show();
		// }
		//
		// @Override
		// public void onSuccess(ArrayList<Model> object) {
		// if (object != null) {
		// List<Model> list = (ArrayList<Model>) object;
		// for (Model model : list) {
		// Logs.d(model.moduleName);
		// }
		// // Toast.makeText(MainActivity.this, (String) object, 1)
		// // .show();
		// }
		// }
		//
		// }.setReturnType(new TypeToken<ArrayList<Model>>() {
		// }));
		Request request = new Request(
				"http://bcs.duapp.com/ttttttttttttttt/module.json?sign=MBO:08fa4d126ce0e0481f207d607a2be537:tJMVODZxlsxrpFm8zrW%2FY6nSkXE%3D");
		request.setCallBack(new JsonCallback<ArrayList<Model>>() {

			@Override
			public void onSuccess(ArrayList<Model> object) {
				for (Model model : object) {
					Logs.d(model.moduleName);
				}
			}

			@Override
			public void onFailure(AppException exception) {
				Toast.makeText(MainActivity.this, "请求失败", 0).show();
			}
		});
		request.execute();
	}

	public void getjsonToObject(View view) {
		Request request = new Request(
				"http://bcs.duapp.com/ttttttttttttttt/moduleClass.json");
		request.setCallBack(new JsonCallback<Model>() {

			@Override
			public void onSuccess(Model object) {
				Toast.makeText(MainActivity.this, object.moduleName, 0).show();
			}

			@Override
			public void onFailure(AppException exception) {

			}
		});
		request.execute();
	}

	public void downloadAndUpdateProgress(View view) {
		String path = Environment.getExternalStorageDirectory()
				.getAbsolutePath() + File.separator + "l.apk";
		// Request request = new Request(
		// "http://bcs.duapp.com/ttttttttttttttt/module.json?sign=MBO:08fa4d126ce0e0481f207d607a2be537:tJMVODZxlsxrpFm8zrW%2FY6nSkXE%3D");
		final Request request = new Request(
				"http://gdown.baidu.com/data/wisegame/55db527e6564f97e/2345liulanqi_62.apk");
		// request.addHeader("Content-Type", "*/*");

		request.setCallBack(new FileCallback() {

			@Override
			public void onSuccess(String object) {
				Toast.makeText(MainActivity.this, "下载成功：" + object, 0).show();
			}

			@Override
			public void onFailure(AppException exception) {
				Toast.makeText(MainActivity.this,
						"下载失败：" + exception.getMessage(), 0).show();
			}

			@Override
			public void onProgress(int curentPos, int totalPos) {
				// if (curentPos > 1000000) { // 1000k
				// request.cancel();
				// }
				// tv.setText(curentPos + ":" + totalPos);
				Logs.d(TAG1, "curentPos：" + curentPos + " totalPos:" + totalPos);
			}

			@Override
			public void onCanceled() {
				Toast.makeText(MainActivity.this, "取消下载成功", 0).show();
			};
		}.setProgress(true).cache(path));
		// request.execute();
		mRequestManager.execute(toString(), request);
	}

	public void downloadAndUpdateProgress1(View view) {
		String path = Environment.getExternalStorageDirectory()
				.getAbsolutePath() + File.separator + "b.apk";
		final Request request = new Request(
				"http://bcs.duapp.com/ttttttttttttttt/b.apk");
		// request.addHeader("Content-Type", "*/*");

		request.setCallBack(new FileCallback() {

			@Override
			public void onSuccess(String object) {
				Toast.makeText(MainActivity.this, "下载成功：" + object, 0).show();
			}

			@Override
			public void onFailure(AppException exception) {
				Toast.makeText(MainActivity.this,
						"下载失败：" + exception.getMessage(), 0).show();
			}

			@Override
			public void onProgress(int curentPos, int totalPos) {
				if (curentPos > 1000000) { // 1000k
					request.cancel(true);
				}
				// tv.setText(curentPos + ":" + totalPos);
				Logs.d(TAG2, "curentPos：" + curentPos + " totalPos:" + totalPos);
			}

			public void onCanceled() {
				Toast.makeText(MainActivity.this, "取消成功", 0).show();
			};
		}.setProgress(true).cache(path));
		// request.execute();
		mRequestManager.execute(toString(), request);
	}

	public void testTimeoutRetry(View view) {
		Request request = new Request(
				"http://bcs.duapp.com/ttttttttttttttt/module.json?sign=MBO:08fa4d126ce0e0481f207d607a2be537:tJMVODZxlsxrpFm8zrW%2FY6nSkXE%3D");
		// request.addHeader("Content-Type", "*/*");
		request.setCallBack(new StringCallback() {

			@Override
			public void onSuccess(String object) {
				Toast.makeText(MainActivity.this, (String) object, 1).show();
			}

			@Override
			public void onFailure(AppException exception) {
				exception.printStackTrace();
				Toast.makeText(MainActivity.this, "请求失败", 0).show();
			}

		}).setRetryCount(3);
		// request.execute();
		mRequestManager.execute(toString(), request);
	}

	public void httpclientPost(View view) {
		try {
			String url = "http://www.wepiao.com/?m=api&c=user&a=login";
			Request request = new Request(url, RequestMethod.POST);
			List<NameValuePair> nvps = new ArrayList<NameValuePair>();
			nvps.add(new BasicNameValuePair("id", "13141111366"));
			nvps.add(new BasicNameValuePair("password", "123456"));
			nvps.add(new BasicNameValuePair("type", "0"));
			// nvps.add(new BasicNameValuePair("openID", "secret"));

			request.entiry = new UrlEncodedFormEntity(nvps);
			request.setCallBack(new StringCallback() {

				@Override
				public void onSuccess(String result) {
					Toast.makeText(MainActivity.this, result + "", 0).show();
				}

				@Override
				public void onFailure(AppException exception) {
					Toast.makeText(MainActivity.this,
							exception.getMessage() + "", 0).show();
				}
			});
			mRequestManager.execute(this.toString(), request);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	public void httpclientGet(View view) {
		String url = "http://www.wepiao.com/?m=api&c=user&a=login&id=13141111366&password=123456&type=0";
		Request request = new Request(url);
		// List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		// nvps.add(new BasicNameValuePair("id", "13141111366"));
		// nvps.add(new BasicNameValuePair("password", "123456"));
		// nvps.add(new BasicNameValuePair("type", "0"));
		// nvps.add(new BasicNameValuePair("openID", "secret"));

		// request.entiry = new UrlEncodedFormEntity(nvps);
		request.setCallBack(new StringCallback() {

			@Override
			public void onSuccess(String result) {
				Toast.makeText(MainActivity.this, result + "", 0).show();
			}

			@Override
			public void onFailure(AppException exception) {
				Toast.makeText(MainActivity.this, exception.getMessage() + "",
						0).show();
			}
		});
		mRequestManager.execute(this.toString(), request);
	}

	public void httpUrlConnectionGet(View view) {
		String url = "http://www.wepiao.com/?m=api&c=user&a=login&id=13141111366&password=123456&type=0";
		Request request = new Request(url);
		request.tool = RequestTool.HTTPURLCONNECTION;
		// List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		// nvps.add(new BasicNameValuePair("id", "13141111366"));
		// nvps.add(new BasicNameValuePair("password", "123456"));
		// nvps.add(new BasicNameValuePair("type", "0"));
		// nvps.add(new BasicNameValuePair("openID", "secret"));

		// request.entiry = new UrlEncodedFormEntity(nvps);
		request.setCallBack(new StringCallback() {

			@Override
			public void onSuccess(String result) {
				Toast.makeText(MainActivity.this, result + "", 0).show();
			}

			@Override
			public void onFailure(AppException exception) {
				Toast.makeText(MainActivity.this, exception.getMessage() + "",
						0).show();
			}
		});
		mRequestManager.execute(this.toString(), request);
	}

	public void httpUrlConnectionPost(View view) {
		String url = "http://www.wepiao.com/?m=api&c=user&a=login";
		Request request = new Request(url, RequestMethod.POST);
		request.tool = RequestTool.HTTPURLCONNECTION;
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("id", "13141111366"));
		nvps.add(new BasicNameValuePair("password", "123456"));
		// nvps.add(new BasicNameValuePair("type", "0"));
		// nvps.add(new BasicNameValuePair("openID", "secret"));

		// try {
		// request.entiry = new UrlEncodedFormEntity(nvps);
		// } catch (UnsupportedEncodingException e) {
		// e.printStackTrace();
		// }
		request.setCallBack(new StringCallback() {
			@Override
			public boolean onCustomParams(OutputStream out) {
				try {
					List<NameValuePair> nvps = new ArrayList<NameValuePair>();
					nvps.add(new BasicNameValuePair("type", "0"));
					nvps.add(new BasicNameValuePair("id", "13141111366"));
					nvps.add(new BasicNameValuePair("password", "123456"));
					UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvps);
					entity.writeTo(out);
					out.flush();
					out.close();
					return true;
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				return false;
			}

			@Override
			public void onSuccess(String result) {
				Toast.makeText(MainActivity.this, result + "", 0).show();
			}

			@Override
			public void onFailure(AppException exception) {
				Toast.makeText(MainActivity.this, exception.getMessage() + "",
						0).show();
			}
		});
		mRequestManager.execute(this.toString(), request);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mRequestManager.cancelForce(toString());
	}

}
